<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_consultation extends BZ_Model
{
	function __construct()
	{
		parent::__construct();
	}


/**
 * @section BISKITZ_FRONT Consultation Widget Model
 * @description the model for biskitz consultation
 */

	/**
	 * @function details
	 * @description the content detail
	 */
	function details
		(
			$prm_dbase = 'db_default'
			, $prm_table_where = '', $prm_content_link_use = TRUE
			, $prm_record_limit = 20
			, $prm_fields_order = ''
			, $prm_table_or_like = ''
			, $prm_excerpt_use = FALSE
			, $prm_title_link_use = 'TRUE'
		)
	{

		$table = array
			(
				'NAME' => 'biskitz_front_module_consultation_content a'
				, 'FIELDS' => 'id, date_format(date, "'.$this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_DATE_TIME_FORMAT'].'") as consultation_date'
						. ', title, content, name, email'
						. ', date_format(answered_date, "'.$this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_DATE_TIME_FORMAT'].'") as answered_date, answered_content, slug, biskitz_user__id'
						. ', (select name from bz_biskitz_user c where c.id = a.biskitz_user__id) as user_name'
						. ', (select name from bz_biskitz_front_module_consultation_category_lang b where a.category__id = b.category__id and language__id = "'.$this->web_data['BISKITZ_FRONT']['LANGUAGE'][0]['id'].'") as filled_under'
			);


		$content_data =	$this->M_database->datas
			(
				$prm_dbase
				, $table['NAME']
				, array_merge
					(
						array
							(
								'language__id' => $this->web_data['BISKITZ_FRONT']['LANGUAGE'][0]['id']
							)
						, $prm_table_where
					)
				, $table['FIELDS']
				, FALSE
				, $prm_fields_order
				, $prm_table_or_like
				, $prm_record_limit, -1
				, FALSE
				, FALSE
			);

		if (is_array($content_data))
		{
			/**
			 * @description load the helpers
			 */
			$this->load->helper
				(
					array
						(
							'text'
						)
				);

			foreach ($content_data as $rows)
			{
				/**
				 * @description generate the content detail
				 */
				if ($prm_excerpt_use == 'TRUE')
				{
					$post_content = sprintf
						(
							$this->lang->line('module_consultation_detail_content_excerpt')
							, htmlentities(word_wrap($rows['name'], $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_POST_COMMENTS_WIDTH']), ENT_QUOTES).'&nbsp;'
							, $rows['filled_under'], $rows['consultation_date']
							, word_limiter
									(
										nl2br(htmlentities(word_wrap($rows['content'], $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_POST_COMMENTS_WIDTH']), ENT_QUOTES))
										, $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_POST_EXCERPT_LIMIT']
									)
						);

				}
				else
				{
					$post_content = sprintf
						(
							$this->lang->line('module_consultation_detail_content_full')
							, htmlentities(word_wrap($rows['name'], $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_POST_COMMENTS_WIDTH']), ENT_QUOTES).'&nbsp;'
							, $rows['filled_under'], $rows['consultation_date']
							, nl2br(htmlentities(word_wrap($rows['content'], $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_POST_COMMENTS_WIDTH']), ENT_QUOTES))
							, $rows['user_name'], $rows['answered_date']
							, $rows['answered_content']
						);
				}

				$post_data[] = array
					(
						/**
						 * @description content detail
						 */
						'id' => $rows['id']
						, 'link' => site_url(array('modules', 'consultation', 'detail', $rows['slug']))
						, 'title' => $rows['title']
						, 'title_link_use' => $prm_title_link_use
						, 'title_tooltips' => sprintf
								(
									$this->lang->line('post_title_tooltips')
									, $rows['title']
								)
						, 'content' => $post_content
/*
						$prm_excerpt_use == 'TRUE' ?
								word_limiter
									(
										$rows['content']
										, $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_POST_EXCERPT_LIMIT']
									)
								: $rows['content']
*/
						, 'user' => $rows['biskitz_user__id']
						, 'posted_by' => $rows['user_name']
						, 'filled_under' => $rows['filled_under']
					);
			}


			$return_value = array
				(
					'POST_DATA' => $post_data
					, 'POST_LINK' => array
						(
							'use' => $prm_content_link_use
							, 'detail_title' => $this->lang->line('post_link_detail')
						)
				);


		}
		else
		{
			$return_value[] = array
				(
					'Error'
					, $this->lang->line('msg_post_no_data')
				);
		}


		return $return_value ;
	}


	/**
	 * @function category_generate
	 * @description generate the category list
	 */
	public function category_generate
		(
			$prm_dbase = 'db_default'
			, $prm_url = array()
			, $prm_language_id = '1'
			, $prm_parent_as_link = TRUE
			, $prm_sub_use = TRUE
			, $prm_content_show = FALSE
			, $prm_counter_use = TRUE
			, $prm_order = 'category_name'
			, $prm_parent_id = 0
			, $prm_parent_tag_open = '<b class="category_parent">', $prm_parent_tag_close = '</b>'
			, $prm_counter_tag_open = '<sup class="category_counter">', $prm_counter_tag_close = '</sup>'
		)
	{
		$category_data =	$this->M_database->datas
			(
				$prm_dbase
				, 'biskitz_front_module_consultation_category a'
				, array
					(
						'parent_id' => $prm_parent_id
					)
				, 'id'
					. ', (select name from bz_biskitz_front_module_consultation_category_lang c where a.id = c.category__id and c.language__id = "'.$prm_language_id.'") as category_name'
					. ', (select slug from bz_biskitz_front_module_consultation_category_lang c where a.id = c.category__id and c.language__id = "'.$prm_language_id.'") as category_slug'
				, FALSE
				, $prm_order
				, ''
				, '', ''
				, FALSE
				, FALSE
			);


		if (is_array($category_data))
		{
			foreach ($category_data as $rows)
			{
				/**
				 * @description is parent as a link?
				 */
				if ($prm_parent_as_link == TRUE)
				{
					$category_anchor = anchor
						(
							site_url(array_merge($prm_url, array($rows['category_slug'])))
							, $rows['category_name']
							, array
								(
									'title' => $rows['category_name']
								)
						);

				}
				else
				{
					$category_anchor = $prm_parent_tag_open . $rows['category_name'] . $prm_parent_tag_close;
				}

				/**
				 * @description need the counter of the content?
				 */
				$counter_count = '';
				if ($prm_counter_use == TRUE)
				{
					$counter_count = $this->M_database->total_rows
						(
							$prm_dbase
							, 'biskitz_front_module_consultation_content'
							, array
								(
									'category__id' => $rows['id']
									, 'language__id' => $prm_language_id
									, 'status' => 'Publish'
								)
						);

					if ($counter_count == 0)
					{
						$counter_count = '';
					}
					else
					{
						$counter_count = $prm_counter_tag_open . $counter_count . $prm_counter_tag_close;
					}
				}


				$return_value[$category_anchor . $counter_count] = $category_anchor . $counter_count;


				/**
				 * @description need to show the content detail?
				 */
				if ($prm_content_show == TRUE)
				{
					$return_value[$category_anchor . $counter_count] = $this->category_list
						(
							$prm_dbase
							, $prm_language_id
							, $rows['id']
						);
				}

				/**
				 * @description need to show the sub category?
				 */
				if ($prm_sub_use == TRUE)
				{
					$category_sub_count = $this->M_database->total_rows
						(
							$prm_dbase
							, 'biskitz_front_module_consultation_category'
							, array
								(
									'parent_id' => $rows['id']
								)
						);

					if ($category_sub_count > 0)
					{
						$category_generate = $this->category_generate
							(
								$prm_dbase
								, $prm_url
								, $prm_language_id
								, $prm_parent_as_link
								, $prm_sub_use
								, $prm_content_show
								, $prm_counter_use
								, $prm_order
								, $rows['id']
								, $prm_parent_tag_open, $prm_parent_tag_close
								, $prm_counter_tag_open, $prm_counter_tag_close
							);

						$return_value[$category_anchor . $counter_count] = ($prm_content_show == TRUE) ?
							array_merge
								(
									$return_value[$category_anchor . $counter_count]
									, $category_generate
								)
							: $category_generate;
					}
				}

			}

			return $return_value;
		}
	}


	/**
	 * @function category_list
	 * @description generate the category type
	 */
	public function category_list($prm_dbase = 'db_default', $prm_language_id = '1', $prm_category_id = 1)
	{
		$use_db = $this->{$prm_dbase};

		$use_db
/*
			->select('title, url')
*/
			->select('title')
			->from('biskitz_front_module_consultation_content')
			->where
				(
					array
						(
							'category__id' => $prm_category_id
							, 'language__id' => $prm_language_id
							, 'status' => 'Publish'
						)
				)
			->order_by('orders');
		$query = $use_db->get();
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $rows)
			{
				$return_value[$rows['title']] = anchor
					(
						$rows['url']
						, htmlentities($rows['title'])
						, array
							(
								'target' => '_blank'
								, 'title' => $rows['title']
							)
					);
			}

		}
		else
		{
			$return_value = array();
		}

		return $return_value;
	}



}

/** End of file m_consultation.php */
/** Location: ./biskitz_mpc/biskitz/models/biskitz/modules/consultation/M_consultation.php */